package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecordKindAdapter;
import flc.ast.adapter.TransferRecordAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferRecordBean;
import flc.ast.databinding.ActivityTransferRecordBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.IntentUtil;

public class TransferRecordActivity extends BaseAc<ActivityTransferRecordBinding> {

    private List<TransferRecordBean> listShow = new ArrayList<>();
    private TransferRecordAdapter recordAdapter;
    private boolean isEdit = false;
    private int type;
    private boolean isSelectAll = false;
    private Dialog myDeleteDialog;
    private List<SelectFileBean> listSel = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.activity_transfer_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTransferRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recordAdapter = new TransferRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
        recordAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (!isEdit) {
                    isEdit = true;
                    recordAdapter.setEdit(isEdit);
                    recordAdapter.notifyDataSetChanged();
                    mDataBinding.llTransferRecordBottom.setVisibility(View.VISIBLE);
                } else {
                    isEdit = false;
                    recordAdapter.setEdit(isEdit);
                    if (type == 1) {
                        getTransferData();
                    } else {
                        getReceiveData();
                    }
                    listSel.clear();
                    mDataBinding.llTransferRecordBottom.setVisibility(View.GONE);
                }
                return true;
            }
        });

        mDataBinding.tvTransferRecord.setOnClickListener(this);
        mDataBinding.tvReceiveRecord.setOnClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.llDelete.setOnClickListener(this);
        mDataBinding.llShare.setOnClickListener(this);
        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvTransferRecord://传送记录
                clearSelection();
                type = 1;
                mDataBinding.tvTransferRecord.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvTransferRecord.setTextSize(18);
                mDataBinding.ivTransferRecord.setVisibility(View.VISIBLE);
                getTransferData();
                break;
            case R.id.tvReceiveRecord://接收记录
                clearSelection();
                type = 2;
                mDataBinding.tvReceiveRecord.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvReceiveRecord.setTextSize(18);
                mDataBinding.ivvReceiveRecord.setVisibility(View.VISIBLE);
                getReceiveData();
                break;
            case R.id.llSelectAll://全选
                isSelectAll();
                break;
            case R.id.llDelete://删除
                myDeleteDialog.show();
                break;
            //删除弹框
            case R.id.tvDialogDeleteConfirm:
                isEdit = false;
                recordAdapter.setEdit(isEdit);
                List<TransferRecordBean> list = recordAdapter.getValidData();
                for (int i = 0; i < list.size(); i++) {
                    TransferRecordBean transferRecordBean = list.get(i);
                    List<SelectFileBean> listKind = transferRecordBean.getList();
                    Iterator<SelectFileBean> iterator = listKind.iterator();
                    while (iterator.hasNext()) {
                        SelectFileBean bean = iterator.next();
                        if (bean.isSelected()) {
                            iterator.remove();
                        }
                    }
                    if (listKind == null || listKind.size() == 0) {
                        list.remove(i);
                    }
                }
                if (type == 1) {
                    AppSpUtils.saveTransferRecordList(list);
                    getTransferData();
                } else {
                    AppSpUtils.saveReceiveRecordList(list);
                    getReceiveData();
                }
                listSel.clear();
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
            case R.id.llShare://分享
                share();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof RecordKindAdapter) {
            RecordKindAdapter kindAdapter = (RecordKindAdapter) adapter;
            SelectFileBean item = kindAdapter.getItem(position);
            if (isEdit) {
                if (item.isSelected()) {
                    item.setSelected(false);
                    listSel.add(item);
                } else {
                    listSel.add(item);
                    item.setSelected(true);
                }
                kindAdapter.notifyDataSetChanged();
            } else {
                if (item.getType() == 1) {
                    ImgDetailActivity.imgPath = item.getPath();
                    startActivity(ImgDetailActivity.class);
                } else if (item.getType() == 2) {
                    VideoDetailActivity.videoPath = item.getPath();
                    startActivity(ImgDetailActivity.class);
                }
            }
        }
    }

    @Override
    protected void initData() {
        getTransferData();
    }

    private void getTransferData() {
        listShow.clear();
        List<TransferRecordBean> listSp = AppSpUtils.getTransferRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getReceiveData() {
        listShow.clear();
        List<TransferRecordBean> listSp = AppSpUtils.getReceiveRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void isSelectAll() {
        if (!isSelectAll) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            for (TransferRecordBean validDatum : recordAdapter.getValidData()) {
                List<SelectFileBean> list = validDatum.getList();
                for (SelectFileBean bean : list) {
                    bean.setSelected(true);
                }
            }
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            for (TransferRecordBean validDatum : recordAdapter.getValidData()) {
                List<SelectFileBean> list = validDatum.getList();
                for (SelectFileBean bean : list) {
                    bean.setSelected(false);
                }
            }
            isSelectAll = false;
        }
        recordAdapter.notifyDataSetChanged();
    }

    private void share() {
        if (listSel.size() == 0) {
            return;
        }
        if (listSel.size() > 1) {
            ToastUtils.showShort(getString(R.string.share_one_file));
            return;
        }
        if (listSel.get(0).getType() == 4 || listSel.get(0).getType() == 5) {
            ToastUtils.showShort(getString(R.string.share_tip));
            return;
        }
        IntentUtil.shareFile(mContext, listSel.get(0).getPath());
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.tvDialogDeleteCancel);
        TextView comp = inflate.findViewById(R.id.tvDialogDeleteConfirm);
        TextView tip = inflate.findViewById(R.id.tvDialogDeleteTip);
        // 设置文本及其颜色
        StringBuffer str = new StringBuffer();
        str.append(getString(R.string.delete_tip1));
        str.append("<font color='#FF3838'>" + getString(R.string.delete_tip2) + "</font>");
        str.append(getString(R.string.delete_tip3));
        // 在文本控件中显示
        tip.setText(Html.fromHtml(str.toString(), getImageGetter(), null));
        cancel.setOnClickListener(this);
        comp.setOnClickListener(this);
    }

    private Html.ImageGetter getImageGetter() {
        return source -> {
            Drawable drawable = getDrawable(Integer.parseInt(source));
            drawable.setBounds(0, 0, 60, 60);
            return drawable;
        };
    }

    private void clearSelection() {
        mDataBinding.tvTransferRecord.setTextSize(15);
        mDataBinding.tvTransferRecord.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.ivTransferRecord.setVisibility(View.INVISIBLE);
        mDataBinding.tvReceiveRecord.setTextSize(15);
        mDataBinding.tvReceiveRecord.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.ivvReceiveRecord.setVisibility(View.INVISIBLE);
    }

}